const { messageModels } =require("../../models");
//获取私信
exports.getMessagePage = async function (req, res) {
    try {
      let data = req.body;
      let count = -1;
      await messageModels
        .getMessagePage(data.pageSize, data.nowPage)
        .then(async (result) => {
          if (data.count) {
            let c = await messageModels.getMessageCount();
            count = c[0].count;
          }
          // if(result.length > 0){
          //   for (let i = 0; i < result.length; i++) {
          //       messageModels.updateMessageStatus(result[i].id)
          //   }
          // }
          res.send({
            code: 200,
            data: {
              result,
              count,
            },
          });
        });
    } catch (error) {
      res.send({
        code: 400,
        data: error,
      });
    }
  };

//获取未读私信总数
exports.getUnreadMessageCount = async function (req, res) {
    try {
     await messageModels.getUnreadMessageCount().then((result) => {
        res.send({
          code: 200,
          data: result,
        });
      });
    } catch (error) {
      res.send({
        code: 400,
        data: error,
      })
    }
}

//删除私信
exports.deleteMessage = async function (req, res) {
    try {
      let data = req.body;
      await messageModels.deleteMessage(data.id).then((result) => {
        res.send({
          code: 200,
          data: result,
        });
      });
    } catch (error) {
      res.send({
        code: 400,
        data: error,
      });
    }
}

//改变私信状态
exports.updateMessageStatus = async function (req, res) {
    try {
      let data = req.body;
      await messageModels.updateMessageStatus(data.id).then((result) => {
        res.send({
          code: 200,
          data: result,
        });
      });
    } catch (error) {
      res.send({
        code: 400,
        data: error,
      })
    }
}